/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog ,datepicker) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'report_page',
                condition: {type: '1'},
                model: {
                    url:_global.url.api + 'report_page',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'修改首页广告',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function() {
                                _this.data.report_id = $.trim($("#report_id").val());
                                _this.data.report_name = $.trim($("#report_name").val());
                                if(_this.data.report_id==''||_this.data.report_id==undefined){
                                    Alert({msg:'请输入曲线id',type:'error'});
                                    return false;
                                }
                                if(_this.data.report_name==''){
                                    Alert({msg:'请输入显示名称',type:'error'});
                                    return false;
                                }
                                _this.data.group =$.trim($("#group").val());
                                if(_this.data.group==''){
                                    Alert({msg:'请输入分组名称',type:'error'});
                                    return false;
                                }
                                _this.data.page_name =$.trim($("#page_name").val());
                                if(_this.data.page_name==''){
                                    Alert({msg:'请输入报表名称',type:'error'});
                                    return false;
                                }
                                _this.data.id = _this.data.id;
                               if(model.put()){
                                   _this.collection.reload();
                               };
                            },cancel: function() {}
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function() {
                            WdatePicker({
                                el:this.id,
                                dateFmt:'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.data.report_id = $.trim($("input[name='report_id']").val());
                _this.data.report_name = $.trim($("input[name='report_name']").val());
                if(_this.data.report_id==''||_this.data.report_id==undefined){
                    Alert({msg:'请输入曲线id',type:'error'});
                    return false;
                }
                if(_this.data.report_name==''){
                    Alert({msg:'请输入显示名称',type:'error'});
                    return false;
                }
                _this.data.group =$.trim($("input[name='group']").val());
                if(_this.data.group==''){
                    Alert({msg:'请输入分组名称',type:'error'});
                    return false;
                }
                _this.data.page_name =$.trim($("input[name='page_name']").val());
                if(_this.data.page_name==''){
                    Alert({msg:'请输入报表名称',type:'error'});
                    return false;
                }
                if(new Model({
                        url: _global.url.api + 'report_page',
                        data: _this.data
                    }).post()) {
                    _this.collection.reload();
                }
            });


        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});